package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bean.dto.Dto;
import com.bean.pojo.*;
import com.bean.vo.ItripLabelDicVO;
import com.bean.vo.comment.*;
import com.service.IItripCommentService;
import com.service.IItripHotelService;
import com.service.IItripImageService;
import com.service.IItripLabelDicService;
import com.util.DtoUtil;
import com.util.PageAssistant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@RestController
public class SystemCommentController {
    @Autowired
    private IItripLabelDicService iItripLabelDicService;
    @Autowired
    private IItripImageService iItripImageService;
    @Autowired
    private IItripCommentService iItripCommentService;
    @Autowired
    private IItripHotelService itripHotelService;

    @GetMapping("/api/comment/gettraveltype")
    public Dto gettraveltype() {
        System.out.println(">>> 查询出游类型列表");

        QueryWrapper<ItripLabelDic> wrapper = new QueryWrapper<>();
        wrapper.eq("parentId",107);
        List<ItripLabelDic> list1 = null;

        try {
            list1 = iItripLabelDicService.list(wrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnError("100019  ", "获取旅游类型列表错误", "");
        }

        List<ItripLabelDicVO> list = new ArrayList<>();

        for (ItripLabelDic idc:
                list1) {
            list.add(new ItripLabelDicVO(idc.getId(),idc.getName()));
        }

        return DtoUtil.returnSuccess("查询成功", list);
    }

    @GetMapping("/api/comment/getimg/{targetId}")
    public Dto getimg(@PathVariable("targetId") String targetId) {
        System.out.println(">>> 根据targetId查询评论照片(type=2)");
        System.out.println("targetId:" + targetId);

        if (targetId == null) {
            return DtoUtil.returnError("100013 ", "评论id不能为空", "");
        }

        ItripImage iimg = new ItripImage();

        QueryWrapper<ItripImage> wrapper = new QueryWrapper<>();
        wrapper.eq("targetId",targetId);
        wrapper.eq("type",2);

        List<ItripImage> list = new ArrayList<>();

        try {
           list = iItripImageService.list(wrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnError("100012  ", "获取评论图片失败", "");
        }

        return DtoUtil.returnSuccess("查询成功", list);
    }

    //3.4.3. 新增评论接口

    //3.4.4. 图片删除接口

    //3.4.5. 图片上传接口

    @PostMapping("/api/comment/getcommentlist")
    public @ResponseBody
    Dto getcommentlist(@RequestBody ItripSearchCommentVO iscv) {
        System.out.println(">>根据评论类型查询评论列表，并分页显示");

        System.out.println(iscv);
        if(iscv.getIsHavingImg()==-1){
            iscv.setIsHavingImg(null);
        }
        if(iscv.getIsOk()==-1){
            iscv.setIsOk(null);
        }
        //需要返回的分页评论类型
        PageAssistant<ItripListCommentVO> getcommentlist = new PageAssistant<>();
        //页面大小
        getcommentlist.setPageSize(iscv.getPageSize());
        //总页数
        getcommentlist.setTotal(iItripCommentService.getcommentlistCount(iscv));
        //当前页
        getcommentlist.setCurPage(iscv.getPageNo());
        //分页类型记录集合
            //处理分页pageSize pageNo  (#{pageNo}-1)*#{pageSize},#{pageSize}
            iscv.setPageNo((iscv.getPageNo()-1)*iscv.getPageSize());
            iscv.setPageSize(iscv.getPageSize());
        System.out.println("输出处理后的分页参数"+iscv.getPageNo()+" "+iscv.getPageSize());

        List<ItripListCommentVO> items = iItripCommentService.getcommentlist(iscv);
        getcommentlist.setRows(items);

        return DtoUtil.returnSuccess("查询成功", getcommentlist);
    }

    @GetMapping("/api/comment/gethoteldesc/{hotelId}")
    public Dto gethoteldesc(@PathVariable("hotelId") String hotelId) {
        System.out.println(">>> 获取酒店相关信息（酒店名称、酒店星级）");
        System.out.println("hotelId:" + hotelId);

        ItripHotelDescVO itripHotelDescVO = null;

        QueryWrapper<ItripHotel> wrapper = new QueryWrapper<>();
        wrapper.eq("id",hotelId);

        try {
            ItripHotel iHotel = itripHotelService.getOne(wrapper);
            System.out.println("酒店信息"+iHotel);
            itripHotelDescVO = new ItripHotelDescVO(iHotel.getId(),iHotel.getHotelName(),iHotel.getHotelLevel());

        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnError("100021  ", "获取酒店相关信息错误", "");
        }

        return DtoUtil.returnSuccess("查询成功", itripHotelDescVO);
    }

    @GetMapping("/api/comment/gethotelscore/{hotelId}")
    public Dto gethotelscore(@PathVariable("hotelId") String hotelId) {
        System.out.println(">>> 据酒店id查询酒店平均分");
        System.out.println("hotelId:" + hotelId);

        if (hotelId == null) {
            return DtoUtil.returnError("100002 ", "hotelId不能为空", "");
        }

        QueryWrapper<ItripComment> wrapper = new QueryWrapper<>();
        wrapper.eq("hotelId",hotelId);
        //根据酒店id查询相应酒店评论
        List<ItripComment> list = iItripCommentService.list(wrapper);

        System.out.println("酒店评论");
        for (ItripComment i:
        list) {
            System.out.println(i);
        }
        //ItripScoreCommentVO
        ItripScoreCommentVO itripScoreCommentVO = iItripCommentService.selectAvgCount(new ItripComment(Long.parseLong(hotelId)));

        return DtoUtil.returnSuccess("查询成功", itripScoreCommentVO);
    }

    @GetMapping("/api/comment/getcount/{hotelId}")
    public Dto getcount(@PathVariable("hotelId") String hotelId) {
        System.out.println(">>> 根据酒店id查询各类评论数量");
        System.out.println("hotelId:" + hotelId);

        if (hotelId == null) {
            return DtoUtil.returnError("100018 ", "参数hotelId为空", "");
        }

        CountVo cv = new CountVo();

        cv.setAllcomment(iItripCommentService.allcommentCount(new ItripComment(Long.parseLong(hotelId))).getAllcomment());
        cv.setImprove(iItripCommentService.improveCount(new ItripComment(Long.parseLong(hotelId))).getImprove());
        cv.setIsok(iItripCommentService.isokCount(new ItripComment(Long.parseLong(hotelId))).getIsok());
        cv.setHavingimg(iItripCommentService.havingimgCount(new ItripComment(Long.parseLong(hotelId))).getHavingimg());

        System.out.println(cv);

        return DtoUtil.returnSuccess("查询成功",cv);
    }

}
